#! /bin/sh

currentDir=$1
domainName=$2
theLocalHostNames=$3
theRelayDomains=$4
theAccess=$5
theAliases=$6
smartHost=$7
luserRelay=$8
relayType=$9
shift
masquerade=$9

cd "$currentDir"

echo ACTIVITY LOG - Sendmail
echo

echo STEP 1. Configuring POP3

echo Turning on POP3 in /etc/inetd.conf
sudo cp /etc/inetd.conf /etc/inetd.conf.cutsaved
sudo sed 's/^ *#* *pop3/pop3/' /etc/inetd.conf.cutsaved > inetd.conf.cuttemp1
sudo sed 's/\/usr\/libexec\/tcpd.*popper.*$/\/usr\/libexec\/tcpd               \/usr\/local\/libexec\/popper/' inetd.conf.cuttemp1 > inetd.conf.cuttemp2
sudo cp inetd.conf.cuttemp2 /etc/inetd.conf
rm inetd.conf.cuttemp*

echo Testing for existence of /usr/local/libexec
if ( test -d /usr/local/libexec ) then
	echo /usr/local/libexec already exists
else
	echo Creating /usr/local/libexec directory
	sudo mkdir /usr/local/libexec
fi

echo Testing for existence of the POP3 server
if ( test /usr/local/libexec/popper -nt popper ) then
	echo /usr/local/libexec/popper already exists
else
	echo Copying popper over to /usr/local/libexec/
	sudo cp popper /usr/local/libexec
fi

echo Testing for existence of the /var/spool/mqueue directory
if ( test -d /var/spool/mqueue ) then
	echo /var/spool/mqueue already exists
else
	echo Creating /var/spool/mqueue directory
	sudo mkdir /var/spool/mqueue
        sudo chmod 750 /var/spool/mqueue
fi

echo Testing for existence of the /var/spool/clientmqueue directory
if ( test -d /var/spool/clientmqueue ) then
	echo /var/spool/clientmqueue already exists
else
	echo Creating /var/spool/clientmqueue directory
	sudo mkdir /var/spool/clientmqueue
        sudo chmod 770 /var/spool/clientmqueue
        sudo chown smmsp:smmsp /var/spool/clientmqueue
fi

echo Turning on POP3 services in NetInfo
sudo nicl / -create /services/pop3 name pop3
sudo nicl / -create /services/pop3 port 110
sudo nicl / -create /services/pop3 protocol tcp

echo Restarting inetd process
sudo kill -HUP `head -1 /var/run/inetd.pid`
echo

echo STEP 2. Configuring Sendmail

echo Setting MAILSERVER=-YES- in /etc/hostconfig
sudo cp /etc/hostconfig /etc/hostconfig.cutsaved
sudo sed 's/MAILSERVER=-NO-/MAILSERVER=-YES-/' /etc/hostconfig.cutsaved > hostconfig.cuttemp
sudo cp hostconfig.cuttemp /etc/hostconfig
rm hostconfig.cuttemp

echo Copying over config.mc
cp config.mc.define config.mc.temp
test "$masquerade" && (echo MASQUERADE_AS\(\`$masquerade\'\) >> config.mc.temp ; echo FEATURE\(\`masquerade_envelope\'\) >> config.mc.temp)
# '

cat config.mc.feature >> config.mc.temp
test "$relayType" = 'OpenRelay' && echo FEATURE\(\`relay_local_from\'\) >> config.mc.temp
# '
test "$smartHost" && echo define\(\`SMART_HOST\', \`$smartHost\'\) >> config.mc.temp
# '
test "$luserRelay" && echo define\(\`LUSER_RELAY\', \`local:$luserRelay\'\) >> config.mc.temp
# '

cat config.mc.mailer >> config.mc.temp
sudo cp config.mc.temp /etc/mail/config.mc
rm config.mc.temp

echo Copying over the update file
sudo cp update /etc/mail

echo Copying over the relay-domains file
echo $theRelayDomains | sudo tr \\r \\n > /etc/mail/relay-domains

echo Copying over the local-host-names file
echo $theLocalHostNames | sudo tr \\r \\n > /etc/mail/local-host-names

echo Copying over the access file
echo $theAccess | sudo tr \\r \\n > /etc/mail/access

echo Copying over the aliases file
echo $theAliases | sudo tr \\r \\n > /etc/mail/aliases

echo Testing for existence of m4
if ( test -f /usr/bin/m4 ) then
    echo /usr/bin/m4 already exists
else
    echo Copying m4 over to /usr/bin/
    sudo cp m4 /usr/bin/
fi

cd /etc/mail
sudo chmod 744 update
sudo ./update
echo

echo STEP 3. ALL DONE.
echo


